/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.unit;

import com.kmcsoft.core.service.GenericService;
import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.model.master.Unit;
import com.kmcsoft.utils.i18n;
import org.zkoss.zk.ui.WrongValueException;

/**
 *
 * @author kmcsoft02
 */
public class UnitComposer extends GenericController<Unit, GenericService> {

    public void onCreate() {
        onClick$new(null);
    }

    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        Unit unit = service.getObject(objectClass, "name.toLowerCase()==pName", "String pName", selected.getName().toLowerCase());
        if (unit != null) {
            if (unit.getId() != selected.getId()) {
                throw new WrongValueException(i18n.l("shop.unit.error.name"));
            }
        }
//        if((selected.getEquivalentUnit()!=null)&&(selected.getAmount()==0)){
//            throw new WrongValueException(i18n.l("shop.unit.error.amount"));
//        }
//        if((selected.getEquivalentUnit()==null)&&(selected.getAmount()>0)){
//            throw new WrongValueException(i18n.l("shop.unit.error.EquivalentUnit"));
//        }
    }

    @Override
    protected void doBeforeDelete() {
        if(selected.isReadonly()){
            throw new WrongValueException(i18n.l("shop.unit.error.delete"));
        }
    }
    
}
